﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeInterop = Microsoft.Office.Interop;

namespace OfficeExport
{
    public class ExportorFactory
    { 
        #region static Methods

        public static object GetOpenFileFormat<T>(string fileExtension)
            where T : IExportor
        {
            if (typeof(WordExportor) == typeof(T))
            { 
                switch(fileExtension)
                {
                    case ".txt":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatText;
                    case ".doc":
                        return Type.Missing;
                    case ".docx":
                        return Type.Missing;
                    case ".rtf":
                        return OfficeInterop.Word.WdOpenFormat.wdOpenFormatRTF;
                    default:
                        return Type.Missing;
                }
            }
            if (typeof(ExcelExportor) == typeof(T))
            {
                switch (fileExtension)
                {
                    case ".xls":
                        return OfficeInterop.Excel.XlFileFormat.xlWorkbookDefault;
                    case ".xlsx":
                        return OfficeInterop.Excel.XlFileFormat.xlWorkbookDefault;
                    default:
                        return Type.Missing;
                }
            }

            return Type.Missing;
        }
        
        public static IExportor GetExportor(string fileExtension)
        {
            string[] wordExtension = new string[] { ".txt", ".doc", ".docx", ".rtf" };
            string[] excelExtension = new string[] { ".xls", ".xlsx" };
            if (wordExtension.Contains(fileExtension.ToLower()))
            {
                return new WordExportor();
            }
            if(excelExtension.Contains(fileExtension.ToLower()))
            {
                return new ExcelExportor();
            }
            return new WordExportor();
        }

        #endregion

        #region Assist Methods

        public static string GetFileExtension(string fileName)
        {
            string fileExtension = string.Empty;
            int index = fileName.LastIndexOf(".");
            if (index > -1)
            {
                fileExtension = fileName.Substring(index, fileName.Length - index);
            }
            return fileExtension;
        }

        public static string GetExportFileExtension(ExportType exportType)
        {
            switch (exportType)
            {
                case ExportType.PDF:
                    return Constants.PDFExtension;
                case ExportType.XPS :
                    return Constants.XPSExtension;
                default:
                    return Constants.PDFExtension;
            }
        }

        public static object GetExportFormat<T>(ExportType exportType)
        {
            if (typeof(WordExportor) == typeof(T))
            {
                switch (exportType)
                {
                    case ExportType.PDF:
                        return OfficeInterop.Word.WdExportFormat.wdExportFormatPDF;
                    case ExportType.XPS:
                        return OfficeInterop.Word.WdExportFormat.wdExportFormatXPS;
                    default:
                        return OfficeInterop.Word.WdExportFormat.wdExportFormatPDF;
                }
            }
            if (typeof(ExcelExportor) == typeof(T))
            {
                switch (exportType)
                {
                    case ExportType.PDF:
                        return OfficeInterop.Excel.XlFixedFormatType.xlTypePDF;
                    case ExportType.XPS:
                        return OfficeInterop.Excel.XlFixedFormatType.xlTypeXPS;
                    default:
                        return OfficeInterop.Excel.XlFixedFormatType.xlTypePDF;
                }
            }
            return 0;
        }

        #endregion
    }
}
